﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OgilvyOne.Model;
using OgilvyOne.DALFactory;
using OgilvyOne.IDAL;
using OgilvyOne.Utility;
using System.Data;
using OgilvyOne.BLL;



namespace OgilvyOne.UKNow.BLL
{
    /// <summary>
    /// 模板标签
    /// </summary>
    public partial class EventsTemplateLabelBll
    {
        private readonly ITemplateLabel dal = TemplateLabelFactory.create();
        public EventsTemplateLabelBll()
        { }
        #region  Method

        //private Hashtable labelmap;
        ///// <summary>
        ///// 获取LABEL MAP
        ///// </summary>
        ///// <returns></returns>
        //public Hashtable GetLabelMap()
        //{
        //    if (labelmap == null || CofcoDynamicSet.isLabelUpdate == true)
        //    {
        //        labelmap = dal.GetLabelHashtable();
        //    }

        //    return labelmap;
        //}

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        private void SaveHtml(TemplateLabelModel model)
        {
            if (model.Ishtml == CmsConstants.YES || (model.HtmlFileName!=null && model.HtmlFileName.IndexOf(".html")>-1))
            {
                CommonModel cmsmodel = CmsProxy.GetModel(model.ModelType, null);
                ICmsLabel cl = LabelProxy.GetCmsLabelLabel(cmsmodel, model.Def); 
                string filename = System.Web.HttpContext.Current.Server.MapPath("~/include/tag/") + model.HtmlFileName;
                FileUtil.SaveHtmlFile(filename, cl.GetLabelHtml());
            }
        }
        /// <summary>
        /// 所有HTML标签生成
        /// </summary>
        public void CreateAllLabelHtml()
        {
            DataSet ds = dal.GetList(" IS_HTML = 1");
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TemplateLabelModel model = new TemplateLabelModel();
                int lid = int.Parse(dt.Rows[i]["LABEL_ID"].ToString());
                model.Id = lid;
                try
                {
                    model.Ishtml = int.Parse(dt.Rows[i]["IS_HTML"].ToString());
                    if (model.Ishtml == CmsConstants.YES) model.HtmlFileName = dt.Rows[i]["HTML_FILE_NAME"].ToString();
                }
                catch { }
                
                model.Def = dt.Rows[i]["LABEL_DEF"].ToString();
                model.Value = dt.Rows[i]["LABEL_VALUE"].ToString();
                try
                {
                    model.ModelType = int.Parse(dt.Rows[i]["MODEL_TYPE"].ToString());
                }
                catch { }
                if (dt.Rows[i]["LABEL_TYPE"] != null) model.LabelType = dt.Rows[i]["LABEL_TYPE"].ToString();

                SaveHtml(model);
            }
        }

        #endregion  Method
    }
}
  